package main

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
} 

func maxLength( arr []int ) int {
    if len(arr) == 0 {
        return 0
    }
    lastOccur := make(map[int]int) // 键存储arr[i],值存储arr[i]最后一次出现的位置
    left := 0
    res := 0
    for right, num := range arr {
        if pos, ok := lastOccur[num]; ok && pos >= left {
            left = pos + 1
        } 
        lastOccur[num] = right
        res = max(res, right - left + 1)
    } 
    return res
}